<?php
/* 
* @Author: Administrator
* @Date:   2014-06-15 15:42:38
* @Last Modified by:   Administrator
* @Last Modified time: 2014-07-17 23:51:02
*/

class UserModel extends Model{
	public $table = 'user';
	// array(' 字段名 ',' 验证方法 ',' 错误信息 ',' 验证条件 ',' 验证时机 '
	public $validate = array(
		array('username','nonull','用户名不能为空',2,1),
		array('username','user:5,20','用户名格式错误，必须5-20个字符以字母开头',2,1),
		// array('username','_isunique','用户名已经被注册',2,1),
		array('usermail','nonull','邮箱不能为空',1,1),
		array('usermail','email','邮箱格式错误',1,1),
		array('usermail','_isunique','邮箱已经被注册',1,1),
		array('password','nonull','用户密码不能为空',1,1),
		array('password','minlen:6','用户密码不得少于6位',1,1),
		array('passwordc','confirm:password','两次密码不相同',1,1),
		array('deal','regexp:/^1$/','必须同意协议才能进行下一步',2,1)
		);
	
	//自动完成
	public $auto = array(
		array('password','_md5','method',2,1),
		array('registertime','_regtime','method',2,1),
		array('rid','_rid','method',2,1),
		array('loginip','_ip','method',2,1),
		array('logintime','_time','method',2,1)
		);

	public function _isunique($name, $value, $msg, $arg)
	{
		if($this->field($name)->where(array($name=>$value))->find()){
			return $msg;
		}
		return true;
	}

	public function _md5($con)	
	{
		return md5($con);
	}


	public function _regtime()
	{
		return time();
	}

	public function _ip()
	{
		return Ip::getClientIp();
	}


	public function _rid(){
		$rid = Q('post.rid',0,'intval');
		if ($rid == 1) {
			return 1;
		}
		return 2;
	}


	public function add_user(){
		return $this->add();
	}

	/**
	 * 验证登录信息
	 * @return [type] [description]
	 */
	public function validate_login()
	{
		$username = Q('post.username');

		$userInfo = $this->where(array('username'=>$username))->find();
		#判断用户名是否存在
		if(!$userInfo){
			$this->error='用户名不存在!';
			return false;
		}
		#接受密码
		$password = Q('post.password',NULL,'md5');
		// echo $password;
		if($userInfo['password'] != $password){
			$this->error='用户名或密码错误!';
			return false;
		}

		if($userInfo['islock']==1){
			$this->error='用户被锁定，如需解锁请联系管理员!';
			return false;
		}

		return $userInfo;
	}


	public function update_login()
	{
		$data = array(
			'loginip'	=>	Ip::getClientIp(),
			'logintime'	=>	time()
			);
		// p($data);exit;
		$this->where(array('uid'=>session('uid')))->save($data);
	}


	public function is_unique_username()
	{
		$username = Q('username');
		if($this->field('username')->where(array('username'=>$username))->find()){
			return false;
		}
		return true;
	}

	/**
	 * 密码修改
	 * @return [type] [description]
	 */
	public function update_password()
	{
		$password1 = Q('post.password1');
		if (strlen($password1) < 6) {
			$this->error = '密码不能少于6位';
			return false;
		}

		$password2 = Q('post.password2');
		if ($password1 != $password2 ) {
			$this->error = '两次密码不相同';
			return false;
		}

		$oldpwd = Q('post.oldpwd','','md5');
		$where = array('uid' => session('uid'));
		$dbpwd = $this->where($where)->getField('password');
		if($oldpwd != $dbpwd){
			$this->error = '原密码错误';
			return false;
		}
		$this->where($where)->update(array('password'=>md5($password1)));
		return true;
	}


	public function get_all()
	{
		return $this->all();
	}

	/**
     * 改变审核状态
     * @return [type] [description]
     */
    public function change_verify($where,$verify){
    	return $this->where($where)->save(array('islock'=>$verify));
    	// p($this->getLastSql());
    }

    public function del_user($where)
    {
    	return $this->where($where)->del();
    }
}

?>
